package academic.ui.client.presenter;

import academic.ui.client.AppServiceAsync;
import academic.ui.client.place.Tokens;
import ecommerce.soco.shared.GWT_Product;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class ReceiptPresenter extends Presenter<ReceiptPresenter.MyView,ReceiptPresenter.MyProxy>{

	private String productID;
	private String quantity;
	
	private AppServiceAsync service;
	private PlaceManager placeManager;
	
	@Inject
	public ReceiptPresenter(EventBus eventBus, MyView view, MyProxy proxy,AppServiceAsync service,
			PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.service=service;
		this.placeManager=placeManager;
	}

	public interface MyView extends View{

		void setup(GWT_Product product, String quantity);

		HasClickHandlers getCloseButton();

		void hide();
	}
	
	@ProxyStandard	
	@NameToken(Tokens.receipt)
	public interface MyProxy extends ProxyPlace<ReceiptPresenter> {
		
	}
	
	public void onBind() {
		super.onBind();
		Log.debug("ReceiptPresenter onBind");
		registerHandler(getView().getCloseButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getView().hide();
				placeManager.revealPlace(new PlaceRequest(Tokens.productFull).with("id",productID));
			}
			
		}));
	}
	
	public void onReveal() {
		super.onReveal();
		service.getFullProduct(Integer.parseInt(productID), new AsyncCallback<GWT_Product>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GWT_Product result) {
				getView().setup(result, quantity);
			}
			
		});
		Log.debug("ReceiptPresenter onReveal");
		
	}
	public void onReset() {
		super.onReset();
		Log.debug("ReceiptPresenter onReset");
	}
	public void onHide() {
		super.onHide();
		Log.debug("ReceiptPresenter onHide");
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, PrimaryPresenter.slot, this);
		Log.debug("ReceiptPresenter revealInParent");
	}
	
	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);
		productID=request.getParameter("id",null);
		if(request.getParameter("quantity", null) != null)
		{
			quantity=request.getParameter("quantity", null);
		}
	}

}
